import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle; 

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher; 

import com.pcbly.web.bean.system.ProvinceCityBean;
import com.pcbly.web.components.utils.random.GUID;

public class Test {
	private String getMacAddress() {
		String address = "";
		String os = System.getProperty("os.name");

		if ((os != null) && os.startsWith("Windows")) {
			try {
				String command = "cmd.exe /c ipconfig /all";
				Process p = Runtime.getRuntime().exec(command);
				BufferedReader br = new BufferedReader(new InputStreamReader(p
						.getInputStream()));
				String line;

				while ((line = br.readLine()) != null) {
					if (line.indexOf("Physical Address") > 0) {
						int index = line.indexOf(":");
						index += 2;
						address = line.substring(index);

						break;
					}
				}

				br.close();

				return address.trim();
			} catch (IOException e) {
			}
		}

		return address;
	}

	private String md5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public void auth() {
		String serverMacAddress = getMacAddress();
		System.out.println(md5(serverMacAddress));
	}
	protected Connection getConnection() throws Exception{
		ResourceBundle rb=ResourceBundle.getBundle("config/jdbc"); 
		String className=rb.getString("jdbc.driverClassName");
		String url=rb.getString("jdbc.url");
		String username=rb.getString("jdbc.username");
		String password=rb.getString("jdbc.password");
		Class.forName(className); 
		return DriverManager.getConnection(url, username,password);
	}
	public void read() {
		try {
			System.out.println("Begin ................ ");
			File file = new File("c:/test.txt");
			FileInputStream fis = new FileInputStream(file); 
			byte[] bys = new byte[1024];
			int len;
			
			StringBuilder sb = new StringBuilder();
			while ((len = fis.read(bys)) > -1) {
				sb.append((new String(bys, 0, len)).toLowerCase());
			} 
			String temp=sb.toString(); 
			String reg=">(^[\\u4e00-\\u9fa5^<]|[^<])*<";
			PatternCompiler compiler = new Perl5Compiler();
			PatternMatcher matcher = new Perl5Matcher();
			Pattern pattern = compiler.compile(reg);
			List<ProvinceCityBean> parents = new ArrayList<ProvinceCityBean>();
			List<ProvinceCityBean> childs = new ArrayList<ProvinceCityBean>();
			PatternMatcherInput input = new PatternMatcherInput(temp);
			String parentId="";
			while (matcher.contains(input, pattern)) {
				MatchResult result = matcher.getMatch(); 
				String resultString=result.group(0);
				if(resultString!=null&&resultString.length()>4){
					ProvinceCityBean bean=new ProvinceCityBean();
					String id=(new GUID()).toString();
					bean.setId(id); 
					boolean parentFlag=false;
					String tempString=resultString.substring(1,7);
					if(tempString.length()>5){
						if(tempString.indexOf("0000")>=0){
							bean.setParentId("0");
							parentId=id;
							bean.setCode(tempString);
							parentFlag=true;
						}else{ 
							bean.setParentId(parentId);
							bean.setCode(tempString);
						}
					}
//					System.out.println(tempString );
					tempString=resultString.substring(8,resultString.length()-1);
					tempString=new String(tempString.getBytes(),"UTF-8");
					bean.setName(tempString);
//					System.out.println(tempString );
					if(parentFlag){
						parents.add(bean);
					}else{
						childs.add(bean);
					}
				}				
			}
 
			if (fis != null) {
				fis.close();
			} 
			Connection conn=getConnection();
			if(conn!=null){
//				conn.setAutoCommit(false);
				String sql="insert into sys_province_city (ID,CODE,NAME,PARENTID)values(?,?,?,?)";
				PreparedStatement state=null;
			    for(ProvinceCityBean parent:parents){
			    	state=conn.prepareStatement(sql);
			    	System.out.println(parent.getName() );
			    	state.setString(1, parent.getId());
			    	state.setString(2, parent.getCode());
			    	state.setString(3, parent.getName());
			    	state.setString(4, parent.getParentId());
			    	state.executeUpdate();
			    }
			    
			    for(ProvinceCityBean parent:childs){
			    	state=conn.prepareStatement(sql);
			    	state.setString(1, parent.getId());
			    	state.setString(2, parent.getCode());
			    	state.setString(3, parent.getName());
			    	state.setString(4, parent.getParentId());
			    	state.executeUpdate();
			    }
			    
//			    conn.commit();
			    if(state!=null){
			    	state.close();
			    }
			    if(conn!=null){
			    	conn.close();
			    }
			}
			System.out.println("End ................ ");
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}

	public static void main(String[] args) {
		Test test = new Test();
		test.auth();
	}
}
